<?php
class Controller_Admin_Weekdata extends Controller_Template
{
	public function __construct($res, $req)
	{
		parent::__construct($res, $req);
		header("Content-Type:text/html;charset=utf-8"); 
		$this->template	= ('smarty:admin/home/default');
	}

	//默认本周数据  上周日到这周末  默认当前时间周日
	public function action_index()
	{
		$last_week	= $this->get_last_week();

		$week_time	= $last_week['time'];

		$s_time	= $week_time['0'];

		$e_time	= $week_time['1'];

		$week	= $last_week['w'];
		
		//周订单数  付过款的订单（包括退款的） ，剔除测试1分钱的订单
		$week_data	= $this->get_orders($s_time, $e_time);
		
		//时间段汇总数据 	
		$data_1		= $this->week_statis_data($week_data);
		$v_data_1[]	= $data_1;	

		//周  店铺 uv，pv，咨询量，商品收藏、店铺收藏
		$data_2			= $this->week_statis_uvdata($s_time, $e_time);
		$v_data_2[]   	= $data_2;  

		//3 .Groupby店铺 , 及按分类汇总的 店铺数据
		$data_3			= $this->week_statis_shop_data($week_data);
		$v_data_3[]		= $data_3;
		
		//地域 汇总数据，groupby省份 
		$province_data	= $this->get_province_orders($s_time, $e_time);
		$v_data_4[]		= $province_data;

		//按“成交金额”分类汇总订单数量、总金额
		$money_data			= $this->week_statis_money_data($week_data);
		$v_data_5[]			= $money_data;
		
		//累计购买次数统计
		$buy_count			= $this->get_end_orders($e_time);
		$data_6				= $this->week_statis_user_data($buy_count);
		$v_data_6[]         = $data_6;
		
		//新老客户消费统计
		$new_old_user			= $this->get_statis_new_old_data($week_data);

		$v_data_7[]             = $new_old_user;

		$file_path = '/home/jingshi/last_week_statisdata.csv';

		$handle = fopen($file_path,'w');

		$str	= '时间段汇总数据 :'."\r\n";

		$str	.= $this->handle_data($v_data_1, $week);
		
		$str	.= '周  店铺 uv，pv，咨询量，商品收藏、店铺收藏 :'."\r\n";

		$str	.= $this->handle_data_2($v_data_2, $v_data_3, $week);
	
		$str	.= 'Groupby店铺 , 及按分类汇总的 店铺数据 :'."\r\n";

		$str	.= $this->handle_data_3($v_data_3, $week);

		$str	.= '地域 汇总数据，groupby省份 :'."\r\n";

		$str	.= $this->handle_data_4($v_data_4, $week);

		$str	.= '按“成交金额”分类汇总订单数量、总金额:'."\r\n";

		$str	.= $this->handle_data_5($v_data_5, $week);

		$str	.= '累计购买次数统计 :'."\r\n";

		$str	.= $this->handle_data_6($v_data_6, $week);

		$str	.= '新老客户消费统计 :'."\r\n";

		$str    .= $this->handle_data_7($v_data_7, $week);

		$users_data	= $this->get_users_order();

		$str	.= '购买次数大于3或者单笔订单金额大于1w的用户订单统计 :'."\r\n";
			
		$str	= str_replace('·','', $str);

		$str	.= $this->handle_users_order($users_data, $week);
		
		$str	= iconv('utf-8', 'gb2312',$str);
		
		fwrite($handle,$str);
		
		fclose($handle);
				
		$emails	= $this->user_email();

		for($i=0; $i< count($emails); $i++)
		{
			$email	= $emails[$i];

			notice::factory('email')->to($email)->subject('搜狐装修网周数据汇总')->message('周汇总数据')->attachment('/home/jingshi/last_week_statisdata.csv')->send();	
		}

	}
	
	private function get_last_week_1()
	{
		return Kohana::config('statis.week_time');
	}

	public function get_last_week()
	{
		$s_time		 = strtotime('2011-09-11');

		$week_s_time = strtotime('last Sunday');
		
		$week_e_time = strtotime('+7 day', $week_s_time);

		$w			 = ceil(($week_e_time - $s_time)/(7*24*60*60));
		
		$array_week	 = array($week_s_time, $week_e_time);

		return array('time' => $array_week, 'w' => $w);
	}

	public function action_total()
	{
		$week	= $this->array_week_config();

		for($i=0; $i< count($week); $i++)
		{
			$s_time	= $week[$i]['0'];

			$e_time	= $week[$i]['1'];

			//周订单数  付过款的订单（包括退款的） ，剔除测试1分钱的订单
			$week_data	= $this->get_orders($s_time, $e_time);
			
			//时间段汇总数据 	
			$data_1		= $this->week_statis_data($week_data);
			$v_data_1[]	= $data_1;	

			//周  店铺 uv，pv，咨询量，商品收藏、店铺收藏
			$data_2			= $this->week_statis_uvdata($s_time, $e_time);
			$v_data_2[]   	= $data_2;  

			//3 .Groupby店铺 , 及按分类汇总的 店铺数据
			$data_3			= $this->week_statis_shop_data($week_data);
			$v_data_3[]		= $data_3;
			
			//地域 汇总数据，groupby省份 
			$province_data	= $this->get_province_orders($s_time, $e_time);
			$v_data_4[]		= $province_data;

			//按“成交金额”分类汇总订单数量、总金额
			$money_data			= $this->week_statis_money_data($week_data);
			$v_data_5[]			= $money_data;
			
			//累计购买次数统计
			$buy_count			= $this->get_end_orders($e_time);
			$data_6				= $this->week_statis_user_data($buy_count);
			$v_data_6[]         = $data_6;
			
			//新老客户消费统计
			$new_old_user			= $this->get_statis_new_old_data($week_data);

			$v_data_7[]             = $new_old_user;

		}

		$file_path = '/home/jingshi/week_data.csv';

		$handle = fopen($file_path,'w');

		$str	= '时间段汇总数据 :'."\r\n";

		$str	.= $this->handle_data($v_data_1);
		
		$str	.= '周  店铺 uv，pv，咨询量，商品收藏、店铺收藏 :'."\r\n";

		$str	.= $this->handle_data_2($v_data_2, $v_data_3);
	
		$str	.= 'Groupby店铺 , 及按分类汇总的 店铺数据 :'."\r\n";

		$str	.= $this->handle_data_3($v_data_3);

		$str	.= '地域 汇总数据，groupby省份 :'."\r\n";

		$str	.= $this->handle_data_4($v_data_4);

		$str	.= '按“成交金额”分类汇总订单数量、总金额:'."\r\n";

		$str	.= $this->handle_data_5($v_data_5);

		$str	.= '累计购买次数统计 :'."\r\n";

		$str	.= $this->handle_data_6($v_data_6);

		$str	.= '新老客户消费统计 :'."\r\n";

		$str    .= $this->handle_data_7($v_data_7);

		$users_data	= $this->get_users_order();

		$str	.= '购买次数大于3或者单笔订单金额大于1w的用户订单统计 :'."\r\n";
			
		$str	= str_replace('·','', $str);

		$str	.= $this->handle_users_order($users_data);
		
		$str	= iconv('utf-8', 'gb2312',$str);

		fwrite($handle,$str);
		
		fclose($handle);
				
		$emails	= $this->user_email();

		for($i=0; $i< count($emails); $i++)
		{
			$email	= $emails[$i];

			notice::factory('email')->to($email)->subject('搜狐装修网累计周数据汇总')->message('累计周汇总数据')->attachment('/home/jingshi/week_data.csv')->send();	
		}

	}
	
	private function handle_data($v_data_1, $week = '')
	{
		$str .= '周, 8-12订单数,流水,12-18订单数,流水,18-24订单数,流水,0-8订单数,流水'."\r\n";

		for($i=0; $i< count($v_data_1); $i++)
		{
			$w		= intval($week)>0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w.','.intval($v_data_1[$i]['08:00:00']['orders']).','.floatval($v_data_1[$i]['08:00:00']['money']/100);	
	
			$str	.= ','.intval($v_data_1[$i]['12:00:00']['orders']).','.floatval($v_data_1[$i]['12:00:00']['money']/100);

			$str    .= ','.intval($v_data_1[$i]['18:00:00']['orders']).','.floatval($v_data_1[$i]['18:00:00']['money']/100);      

			$str	.= ','.intval($v_data_1[$i]['00:00:00']['orders']).','.floatval($v_data_1[$i]['00:00:00']['money']/100)."\r\n"; 
		}
		return $str;
	}

	private function handle_data_2($v_data_2, $v_data_3, $week = '')
	{
		for($i=0; $i< count($v_data_2); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
		
			$str .= '名称,pv, uv,店铺咨询, 商品收藏, 店铺收藏, 订单数， 流水'."\r\n";
			
			foreach($v_data_2[$i] as $key => $val)
			{
				$shop_id	= $val['shop_id'];

				$orders		= intval($v_data_3[$i]['shops'][$shop_id]['orders']);

				$money		= floatval($v_data_3[$i]['shops'][$shop_id]['money']/100);

				$str .= trim($val['shop_name']).','.$val['pv'].','.$val['uv'].','.$val['shop_consult'].','.$val['goods_fav'].','.$val['shop_fav'].','.$orders.','.$money."\r\n";
			}	
					
		}
		return $str;
	}

	private function handle_data_3($v_data_3, $week = '')
	{
		for($i=0; $i< count($v_data_3); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
			
			$str .= '名称, 订单数, 流水'."\r\n";
/*
			foreach($v_data_3[$i]['shops'] as $key => $val)
			{
				$shop_name	= Shop::name($key);

				$str .= trim($shop_name).','.$val['orders'].','.floatval($val['money']/100)."\r\n"; 
			}
*/
			foreach($v_data_3[$i]['cates'] as $key => $val)
            {
                if($key == 'shenghuo')
					$type_name	= '生活'; 

				if($key == 'weiyu')
					$type_name	= '卫浴';

				if($key == 'jiaju')
					$type_name  = '家具';

				if($key == 'jiancai')
					$type_name  = '建材';

                $str .= $type_name.','.$val['orders'].','.floatval($val['moneys']/100)."\r\n";
            }
		
		}
		return $str;
	}

	private function handle_data_4($v_data_4, $week = '')
	{
		for($i=0; $i< count($v_data_4); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
		
			$str .= '订单数, 流水'."\r\n";

			for($k=0; $k< count($v_data_4[$i]); $k++)
			{
				$province_name  = City::province_name($v_data_4[$i][$k]['province']);

				$str .= $province_name.','. $v_data_4[$i][$k]['count'].','.floatval($v_data_4[$i][$k]['money']/100)."\r\n";
			}
		}
		return $str;
	}

	private function handle_data_5($rs_data, $week = '')
	{
		for($i=0; $i< count($rs_data); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
	
			$str .= '订单数, 流水'."\r\n";

			foreach($rs_data[$i] as $key => $val)
			{
				if($key == 0)
					$name	= '小于等于1000';
				if($key == 1000)
					$name	= '大于1000小于等于5000';
				if($key	== 5000)
					$name	= '大于5000';

				$str .= $name.','.$val['orders'].','.floatval($val['money']/100)."\r\n";
			}
		}
		return $str;
	}

	private function handle_data_6($rs_data, $week = '')
	{
		for($i=0; $i< count($rs_data); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
			
			$str .= '购买次数汇总：'."\r\n";
			$str .= '类型, 订单数, 流水, 用户数'."\r\n";
			foreach($rs_data[$i] as $k => $v)
			{
				if($k == 1 )
					$names	= '购买一次';
				elseif($k == 2)
					$names	= '购买2-5次';
				elseif($k == 6)
					$names	= '购买6次以上';

				$str .= $names.','.$v['orders'].','.floatval($v['money']/100).','.$v['users']."\r\n";
			}

		}
		return $str;
	}

	private function handle_data_7($v_data_7, $week = '')
	{
		for($i=0; $i< count($v_data_7); $i++)
		{
			$w		= intval($week) > 0 ? intval($week) : ($i+1);

			$str	.= 'w'.$w."\r\n";	
			
			$str .= '用户类型, 用户数, 订单数, 流水'."\r\n";

			foreach($v_data_7[$i] as $key => $val)
			{
				if($key == 'all')
					$name	= '本周总计';
				elseif($key == 'new')
					$name	= '新客';
				elseif($key == 'old')
					$name	= '老客';

				$str .= $name.','.$val['users'].','. $val['orders'].','.floatval($val['money']/100)."\r\n";	 
			}
		}
		return $str;
	}

	private function handle_users_order($orders_data, $week = '')
	{
		$str	.= intval($week) > 0 ? intval($week) : '';	
		$str	.= "\r\n".'类型,用户,商铺名称, 订单数, 付款时间, 金额, 订单id, 份数, 地址, 显示订单id'."\r\n";
		
		for($i=0; $i< count($orders_data); $i++)
		{
			if($orders_data[$i]['orders'] >= 3)
				$name = '订单数大于3';
			else
				$name = '金额大于1w';

			$str .= $name.','.User::name($orders_data[$i]['uid']).','.$orders_data[$i]['shop'].','.$orders_data[$i]['orders'].','.$orders_data[$i]['pay_time'].','.floatval($orders_data[$i]['money']/100).','.$orders_data[$i]['orders_id'].','.$orders_data[$i]['quantity'].','.$orders_data[$i]['address'].','.$orders_data[$i]['orders_ids']."\r\n";	
		}
		return $str;
	}

	private function get_users_order()
	{
		//剔除测试1分钱数据
		$query	= DB::select(array('count(distinct "o.id")', 'count'))
					->select(array('sum("o.amount_real")', 'sum'))
					->select("o.uid")
					->from(array('order_basic', 'o'))
					->where("o.shop", '!=', "44")
					->where("o.create_time", '>=', strtotime('2011-09-11'))
					->where("o.pay_time", '>', strtotime('2011-09-11'))
					->where("o.status", 'in', array(34, 65, 66, 73))
					->group_by("o.uid")
					->execute('readonly');
		
		$data	= $query->as_array();
		
		$rs_data= array();
		
		for($i=0; $i< count($data); $i++)
		{
			$rs	= array();

			if($data[$i]['count'] >= 3 || $data[$i]['sum'] >= 1000000)
			{
				$order_ids		= $this->get_condition_order_ids($data[$i]['uid']);

				$rs['orders']	= $data[$i]['count'];

				$rs['money']    = $data[$i]['sum'];
		
				$rs['orders_id']= $order_ids['id'];
		
				$rs['orders_ids']= $order_ids['real_id'];

				$rs['shop']		= $order_ids['shop'];

				$rs['pay_time'] = $order_ids['pay_time'];

				$rs['address']  = $order_ids['address'];

				$rs['quantity'] = $order_ids['quantity'];

				$rs['uid']		= $data[$i]['uid'];
			}
			if(! empty($rs))	
				$rs_data[]	= $rs;
		}
		
		return $rs_data;
	}

	
	private function get_condition_order_ids($uid)
	{
		$data	= DB::select('id','shop', 'pay_time')
					->from('order_basic')
					->where('uid', '=', intval($uid))
					->where('status', 'in', array(34,65, 66, 73))
					->where('pay_time', '>', 0)
					->execute('readonly');

		$rs		= $data->as_array();

		$ids	= array();

		for($i=0; $i< count($rs); $i++)
		{
			$order_ids['id']	  .= $rs[$i]['id'].'，';

			$ids[]				  = $rs[$i]['id'];

			$order_ids['real_id'] .= Order_Basic::formated_order_id($rs[$i]['id']).'，';

			$order_ids['shop']	  .= trim(str_replace('·','', shop::name($rs[$i]['shop']))).'， ';

			$order_ids['pay_time'].= date('Y-m-d H:i', $rs[$i]['pay_time']).'， ';
		}
		
		$order_ids['address']	   = $this->get_provice_city($ids);
		
		$order_ids['quantity']	   = $this->get_quantity($ids);

		return $order_ids;	
	}

	private function get_quantity($ids)
	{
		if(empty($ids))
			return 0;
		
		$data	= DB::select(array('sum("quantity")', 'quantity'))	
					->from('order_goods')
					->where('order_id', 'in', $ids)
					->execute('readonly')
					->get('quantity');
		
		return $data;	
	}

	private function get_provice_city($order_ids)
	{
		if(empty($order_ids))
			return '';

		$data	= DB::select('province', 'city', 'address')	
					->from('order_logistics')
					->where('order_id', 'in', $order_ids)
					->execute('readonly');

		$rs		= $data->as_array();

		$address	= '';
		for($i=0; $i< count($rs); $i++)
		{
			$address .= City::province_name($rs[$i]['province']). ' ';

			$address .= City::city_name($rs[$i]['city']). ' ';

			$address .= trim(str_replace("\n", '', $rs[$i]['address'])). ' ，';
		}

		return $address;
	}

	private function get_statis_new_old_data($datas)
	{
		$rs_data	= array();
		
		if(empty($datas))
			return $rs_data;
	
		$users	= array('new' => array(), 'old' => array(), 'all' => array());

		for($i=0; $i< count($datas); $i++)
		{
			$rs_data['all']['orders']	+= 1;

			$rs_data['all']['money']    += $datas[$i]['amount_real'];
			
			$new	= $this->is_new_user($datas[$i]['uid'], $datas[$i]['pay_time']);

			if($new)
			{
				$rs_data['new']['orders']	+= 1;

				$rs_data['new']['money']    += $datas[$i]['amount_real'];

				if(!in_array($datas[$i]['uid'], $users['new']))
					$rs_data['new']['users']   += 1;
				
				$users['new'][]				= $datas[$i]['uid'];

			}else{
				$rs_data['old']['orders']   += 1;

				$rs_data['old']['money']    += $datas[$i]['amount_real'];

				if(!in_array($datas[$i]['uid'], $users['old']))
					$rs_data['old']['users']   += 1;

				$users['old'][]             = $datas[$i]['uid'];
			}

			if(!in_array($datas[$i]['uid'], $users['all']))
				$rs_data['all']['users']   += 1;

			$users['all'][]             = $datas[$i]['uid'];
		}
		//print_r($rs_data);	
		return $rs_data;	
	}

	private function is_new_user($uid, $pay_time)
	{
		$create_time	= DB::select('create_time')
							->from('user_basic')
							->where('uid', '=', $uid)
							->execute('readonly')
							->get('create_time');

		if(($pay_time - $create_time) - (7*24*60*60) <= 0 )
			return TRUE;
		else
			return FALSE;
	}	
	
	private function week_statis_uvdata($s_time, $e_time) 
	{
		$data	= DB::select(array('sum("s.jspv")', 'pv'))		
					->select(array('sum("s.jsip")', 'uv'))
					->select("s.shop_id")	
					->from(array('statis_access', 's'))
					->join(array('shop_baseinfo', 'b'))
					->on("s.shop_id", '=', "b.id")
					->where("s.shop_id", '!=', '44')
					->where("s.ddate", '>=', date('Y-m-d', $s_time))
					->where("s.ddate", '<', date('Y-m-d', $e_time))
					->where("b.open_status", '=', 1)
					->where("s.type", '=', 1)
					->group_by("s.shop_id")
					->execute('readonly');
		
		$rs_data	= $data->as_array();
		
		$target_types   = Kohana::config('statis.target_type');

		$types          = Kohana::config('statis.type');

		$shop_fav   = Model_Admin_Statis::get_behavior_data_type($s_time, $e_time, $target_types['shop'], $types['favorite']);

		$messages   = Model_Admin_Statis::get_behavior_data_type($s_time, $e_time, $target_types['shop'], $types['messages']);
		
		$goods_fav  = Model_Admin_Statis::get_behavior_data_type($s_time, $e_time, $target_types['goods'], $types['favorite']);

		$consult    = Model_Admin_Statis::get_behavior_data_type($s_time, $e_time, $target_types['goods'], $types['consult']);
		
		$con_im     = Kohana::config('statistics.type.IM');

		$ims		= Model_Admin_Statis::get_statis_im_data($s_time, $e_time, $con_im);

		$goods_favs = $this->handle_behavior_data($goods_fav);

		$consults   = $this->handle_behavior_data($consult);
		
		for($i=0; $i< count($rs_data); $i++)		
		{
			$shop_id	= $rs_data[$i]['shop_id'];
		
			$rs_data[$i]['shop_name']		= 	Shop::name($shop_id);

			$rs_data[$i]['shop_fav']		= intval($shop_fav[$shop_id]['num']);

			$rs_data[$i]['goods_fav']    	= intval($goods_favs[$shop_id]['num']);	
			
			$rs_data[$i]['shop_consult'] 	= intval($messages[$shop_id]['num']) + intval($consults[$shop_id]['num']) + intval($ims[$shop_id]['pv']) + intval($ims[$shop_id]['ip']);	
		}
		return $rs_data;
	}

	private function handle_behavior_data($goods_data)
    {

        $rs_data    = array();

        if(count($goods_data) <= 0)
            return $rs_data;

        foreach($goods_data as $key => $val)
        {
            $shop_id    = $val['shop_id'];

            if(! isset($rs_data[$shop_id]))
            {
                $rs_data[$shop_id]['num']   = $val['num'];
            }else{

                $rs_data[$shop_id]['num']   += $val['num'];
            }
        }

        return $rs_data;
    }

	private function week_statis_user_data($week_data)
	{
		$data	= array();
		
		for($i=0; $i< count($week_data); $i++)
		{
			if($week_data[$i]['orders'] == 1)
			{
				$data[1]['users']	 += 1;
			
				$data[1]['orders']   += $week_data[$i]['orders'];

				$data[1]['money']    += $week_data[$i]['amount_real'];
			}
			if($week_data[$i]['orders'] >=2  && $week_data[$i]['orders'] <= 5)
			{
				$data[2]['users']	 += 1;
			
				$data[2]['orders']   += $week_data[$i]['orders'];

				$data[2]['money']    += $week_data[$i]['amount_real'];
			}

			if($week_data[$i]['orders'] >= 6)
			{
				$data[6]['users']	 += 1;
			
				$data[6]['orders']   += $week_data[$i]['orders'];

				$data[6]['money']    += $week_data[$i]['amount_real'];
			}

		}
				
		return $data;
	}

	private function week_statis_money_data($week_data)
	{
		$money_config	= $this->array_money_config();

		$data			= array();

		for($i=0; $i< count($week_data); $i++)
		{
			$money	= ceil($week_data[$i]['amount_real']/ 100 );	
			
			for($k=0; $k<count($money_config); $k++)
			{
				$key	= $money_config[$k]['0'];

				if($money > $money_config[$k][0] && $money <= $money_config[$k][1])
				{
					$data[$key]['money']	+= $week_data[$i]['amount_real'];

					$data[$key]['orders']    += 1;
					break;
				}
			}	
		}
		
		return $data;
	}

	private function week_statis_shop_data($week_data)
	{
		$shop_config	= $this->array_shop_config();

		$rs_data		= array();
		
		$cage_data		= array();
		
		for($i=0; $i< count($week_data); $i++)
		{
			$key	= $week_data[$i]['shop'];

			if(!isset($rs_data[$key]['orders']))
				$rs_data[$key]['orders']	= 0;
	
			if(!isset($rs_data[$key]['money']))
				$rs_data[$key]['money']		= 0;

			$rs_data[$key]['orders']	+= 1;

			$rs_data[$key]['money']    += $week_data[$i]['amount_real'];

			foreach($shop_config as $k => $val)
			{
				if(in_array($key, $val))
				{
					$cage_data[$k]['orders']	+= 1;

					$cage_data[$k]['moneys']    += $week_data[$i]['amount_real'];
				}
			}
		}
		return array('shops' => $rs_data, 'cates' => $cage_data);	
	}

	private function week_statis_data($week_data)
	{
		$d_time		= $this->array_time_config();

		$data		= array();

		for($i=0; $i< count($week_data); $i++)
		{
			$pay_time	= strtotime(date('H:i:s' , $week_data[$i]['pay_time']));
			
			for($k=0; $k< count($d_time); $k++)
			{
				$s_time	= strtotime($d_time[$k][0]);

				$e_time	= strtotime($d_time[$k][1]);
				
				$key	= $d_time[$k][0];
		
				if(!isset($data[$key]['orders']))
					$data[$key]['orders'] = 0;

				if(!isset($data[$key]['money']))
					$data[$key]['money'] = 0;

				if($pay_time >= $s_time && $pay_time < $e_time)
				{
					$data[$key]['orders']	+= 1;

					$data[$key]['money']	+= $week_data[$i]['amount_real'];
					
					break;
				}
			}
		}
		return $data;
	}

	private function get_orders($s_time, $e_time)
	{
		$data	= DB::select(array('distinct "o.id"','id'))
					->select("o.amount_real", "o.uid", "o.shop", "o.create_time", "o.pay_time","o.status")
					->from(array('order_basic', 'o'))
					->where("o.shop", '!=', '44')
					->where("o.create_time",'>=', strtotime('2011-09-02'))
					->where("o.pay_time", '>=', $s_time)
					->where("o.pay_time", '<', $e_time)
					->where("o.status", 'in', array('34', 65, 66, 73))
					->execute('readonly');
		$rs		= $data->as_array();
	//echo $data."<br/>";	
		return $rs;
	}

	private function get_end_orders($e_time)
	{
		$data	= DB::select(array('count("o.id")', 'orders'))
					->select(array('sum("o.amount_real")', 'amount_real'))
					->select("o.uid")
					->from(array('order_basic', 'o'))
					->where("o.shop", '!=', '44')
					->where("o.create_time",'>=', strtotime('2011-09-11'))
					->where("o.pay_time", '<', $e_time)
					->where("o.pay_time", '>=', strtotime('2011-09-11'))
					->where("o.status", 'in', array('34', 65, 66, 73))
					->group_by("o.uid")
					->execute('readonly');
		
		return $data->as_array();
	}

	private function get_province_orders($s_time, $e_time)
	{

		$data	= DB::select(array('count("o.id")', 'count'))
					->select(array('sum("o.amount_real")', 'money'))
					->select("ol.province")
					->from(array('order_basic', 'o'))
					->join(array('order_logistics', 'ol'))
					->on("o.id", '=', "ol.order_id")
					->where("o.shop", '!=', '44')
					->where("o.create_time",'>=', strtotime('2011-09-02'))
					->where("o.pay_time", '>=', $s_time)
					->where("o.pay_time", '<', $e_time)
					->where("o.status", 'in', array('34', 65, 66, 73))
					->group_by("ol.province")
					->execute('readonly');
		
		return $data->as_array();
	}

	public function array_money_config()
	{
		return array(
					array('0', '1000'),
					array('1000', '5000'),
					array('5000', '200000'),
				);
	
	}

	public function array_time_config()
	{
		return array(
					array('08:00:00', '12:00:00'),
					array('12:00:00', '18:00:00'),
					array('18:00:00', '23:59:59'),
					array('00:00:00', '08:00:00'),
				);
	
	}

	public function array_week_config()
	{
		$stime	= strtotime('2011-09-11');
	
		$week_e_time = strtotime('last Sunday');

		$count	= ceil($week_e_time - $stime)/(7*24*60*60);

		$arr_week	= array();

		for($i=0; $i< $count; $i++)
		{
			$week[]	= $stime;

			$week[] = strtotime('+7 day',$stime);

			$arr_week[]	= $week;

			$stime	= strtotime('+7 day',$stime);

			$week	= array();
		}
		return $arr_week;				
	}

	private function user_email()
    {
        return array(
                    'jingshi@sohu-inc.com',
                    /*'nazhao@sohu-inc.com',
                    'tianjiangwen@sohu-inc.com',
                    'yongxingding@sohu-inc.com'*/
                    );
    }

	public function array_shop_config()
	{
		return array(
					'jiaju'		=> array('10040', '71', '74', '10036', '10012'),
					'jiancai'	=> array('10037', '10023', '10021', '70', '10002', '10001', '10000', '10010', '10008'),
					'weiyu'		=> array('73', '75', '10020'),
					'shenghuo'	=> array('10038', '10014', '10011', '10005')
				);
	}
	
}

?>
